<!--
 * @Author: wangming
 * @Date: 2021-03-30 14:16:00
 * @LastEditors: wangming
 * @LastEditTime: 2022-07-12 17:52:13
 * @Description: file content
-->
<template>
  <div class="menu__container">
    <div class="menu__container--inner">
      <el-menu
        unique-opened
        :default-active="activeMenu"
        :collapse="isCollapse"
        :background-color="variables.menuBg"
        :text-color="variables.menuText"
        :active-text-color="variables.menuActiveText"
      >
        <el-submenu
          :index="item.path"
          v-for="(item, index) in getMenusList"
          :key="index"
        >
          <template slot="title">
            <i :class="item.meta.icon" v-if="!!item.meta.icon"></i>
            <span slot="title">{{ item.meta.title }}</span>
          </template>
          <template v-if="!!item.children && item.children.length > 0">
            <el-menu-item
              v-for="i in item.children"
              class="menu__submenu-item"
              @click.native="goPage(`/${item.path}/${i.path}`, i)"
              :index="`/${item.path}/${i.path}`"
              :key="i.path"
            >
              <i :class="i.meta.icon" v-if="!!i.meta.icon" class="lh1"></i>
              <span> {{ i.meta.title }}</span>
            </el-menu-item>
          </template>
        </el-submenu>
      </el-menu>
    </div>
  </div>
</template>

<script>
import variables from "@/assets/sass/variables.scss";
import { mapGetters } from "vuex";
import { Accesstoken } from "@/utils/auth";
import qs from "qs";

export default {
  props: {
    isCollapse: {
      default: false,
      type: Boolean,
    },
  },
  computed: {
    ...mapGetters(["getMenusList"]),
    activeMenu() {
      let { path } = this.$route,
        appId = this.$route.query.paremtMenuId;
      if (!!appId) {
        //app 动态详情页场景
        return appId;
      } else if (/^\/app\//.test(path)) {
        //amdin  场景
        return path;
      } else {
        //app系统页面场景
        return path.replace(/(\/\w+)(\/\w+)(\/\w+)/, "$1$2");
      }
    },
  },
  data() {
    return {
      variables,
    };
  },
  methods: {
    changepath(str, id) {
      return str.replace(/(\/\w+)(\/\w+)/, `$1${id}$2`);
    },
    async goPage(path, i) {
      if (i?.meta?.target === "OUTER") {
        let { address, withToken } = i.meta;
        if (!!withToken) {
          let hashArr = address.split("#");
          let queryArr = hashArr[0].split("?");
          let queryData = qs.parse(queryArr[1]);
          queryData["token"] = Accesstoken.getAccessToken();
          queryArr[1] = qs.stringify(queryData);
          hashArr[0] = queryArr.join("?");
          address = hashArr.join("#");
        }
        window.open(address);
      } else {
        await this.$router.push({
          path,
          query: {
            pageId: i.meta.pageId,
          },
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.el-menu {
  border-right: none;
}

.menu__container {
  overflow: hidden;
  height: calc(100vh - 60px);
  background: var(--hancode-menu-bg-color) !important;
  transition: all 0.5s;

  &--inner {
    height: calc(100vh - 60px);
    width: calc(100% + 10px);
    overflow-y: scroll;
  }

  ::v-deep .el-submenu__title {
    background: var(--hancode-menu-bg-color) !important;
    color: var(--hancode-menu-color) !important;
    transition: all 0.5s;

    &:hover {
      background: var(--hancode-menu-active-bg-color) !important;
      color: var(--hancode-menu-active-color) !important;
    }
  }
}

.menu__submenu-item {
  background: var(--hancode-sub-menu-bg-color) !important;
  color: var(--hancode-menu-color) !important;
  transition: all 0.5s;

  &:hover,
  &.is-active {
    background: var(--hancode-menu-active-bg-color) !important;
    color: var(--hancode-menu-active-color) !important;
  }
}
</style>
